<?php

// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;
use Think\Model;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController
{
    public function index(){
        import('Vendor.Wechat.TPWechat');
        $options = array(
            'token'=>'12345678', //��д���趨��key
            'encodingaeskey'=>'OrK50UpXmPpyQykggqRxhxmesG4tBDH39HkOnOH5UpE', //��д�����õ�EncodingAESKey
            'appid'=>'wx4bd7473a77015cff', //��д�߼����ù��ܵ�app id
            'appsecret'=>'b99322c1ee08f45357b49ca12f7929f8' //��д�߼����ù��ܵ���Կ
        );
        $weobj = new \TPWechat($options);
//        dump($weobj);
//        $weobj->valid();
        $User=M("wxm");
        $ccc=$weobj->getRev()->getRevContent();
        $value=$User->where("value=".$ccc."")->select();
        $yn=$value[0]["yn"];

        $type = $weobj->getRev()->getRevType();
        $fromuser = $weobj->getRev()->getRevFrom();
        trace($weobj->getRev()->getRevData());
//        $weobj->createXml();
//        trace($weobj->text("111")->reply());
        switch($type) {
            case \TPWechat::MSGTYPE_TEXT:

                if($value){
                    if($yn==1){
//                    $aaa=$this->sendRedpack1();
                    //trace($weobj->text("100".$fromuser)->reply());
//                $weobj->text("100".$fromuser)->reply();
                    trace("222222");
//                $result = $weobj->hongbao("100", $fromuser)->reply();
                    $result = $weobj->sendHongbao("100", $fromuser);
//                        $data["yn"]="0";
//                        $data["yh"]=$fromuser;
//                        $User->where("value=".$ccc."")->save($data);
                    }else{
                        $weobj->text("该红包已被领取")->reply();
                    }
                }else{
                    $weobj->text("请输入的红包领取口令有误，请核对后再次领取")->reply();
                }
                exit;
                break;
            case \TPWechat::MSGTYPE_EVENT:
                break;
            case \TPWechat::MSGTYPE_IMAGE:
                break;
            default:
                $weobj->text("help info")->reply();
        }
    }

}
